package com.platform.api;

import com.platform.annotation.IgnoreAuth;
import com.platform.annotation.LoginUser;
import com.platform.entity.*;
import com.platform.service.*;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 评论Controller
 *
 * @author zhurusen
 * @email 524227263@qq.com
 * @date 2018-05-15 15:50:14
 */
@RestController
@RequestMapping("api/comment")
public class ApiCommentController {

    @Autowired
    private ApiUserLabelService apiUserLabelService;
    @Autowired
    private ApiCommentService apiCommentService;
    @Autowired
    private ApiUserService apiUserService;
    @Autowired
    private ApiLabelService apiLabelService;
    @Autowired
    private ApiOrderService apiOrderService;
    @Autowired
    private ApiCircleBillService apiCircleBillService;


    /**
     * 服务评论列表
     */
    @IgnoreAuth
    @RequestMapping("/list")
    public R list(Long userId, @RequestParam(value = "page", defaultValue = "1") Integer page) {
        //查询列表数据
        Map<String, Object> params = new HashMap<>();
        params.put("page", page);
        params.put("limit", 10);
        params.put("commentUserId", userId);

        params.put("sidx", "create_date");
        params.put("order", "desc");
        Query query = new Query(params);

        List<ApiCommentVo> list = apiCommentService.selectList(query);

        List<UserCommentVo> commentVos = new ArrayList<>();
        for (ApiCommentVo info : list) {
            UserCommentVo userCommentVo = new UserCommentVo();
            userCommentVo.setId(info.getId());

            UserVo userVo = apiUserService.queryObject(info.getUserId());
            if (userVo != null) {
                userCommentVo.setAvatar(userVo.getAvatar());
                userCommentVo.setNickName(userVo.getNickname());
            }

            userCommentVo.setCreateDate(info.getCreateDate());
            userCommentVo.setContent(info.getContent());

            Map<String, Object> paramsLabel = new HashMap<>();
            paramsLabel.put("tvAnchor", info.getCommentUserId());
            paramsLabel.put("mouldId", info.getOrderId());

            List<ApiUserLabelVo> labelVos = apiUserLabelService.selectList(paramsLabel);


            List<ApiLabelVo> lableList = new ArrayList<>();

            for (ApiUserLabelVo apiUserLabelVo : labelVos) {
                ApiLabelVo labelVo = apiLabelService.selectOne(apiUserLabelVo.getLabelId());
                if (labelVo != null) {
                    lableList.add(labelVo);
                }
            }
            userCommentVo.setLabels(lableList);

            commentVos.add(userCommentVo);
        }

        return R.ok(0, "获取成功", commentVos);
    }


    /**
     * 提交评论
     *
     * @param loginUser
     * @param userId    评论的主播id
     * @param content
     * @param orderId
     * @param mouldName live  wake  emotion guide
     * @return
     */
    @RequestMapping("/save")
    public R add(@LoginUser UserVo loginUser, Long userId, String content, Long orderId,
                 @RequestParam(value = "mouldName", defaultValue = "") String mouldName, String labelIds) {
        ApiCommentVo apiCommentVo = new ApiCommentVo();
        apiCommentVo.setCreateDate(new Date());
        apiCommentVo.setCommentUserId(userId);
        apiCommentVo.setUserId(loginUser.getId());
        apiCommentVo.setContent(content);
        apiCommentVo.setOrderId(orderId);
        apiCommentVo.setMouldName(mouldName);
        apiCommentService.save(apiCommentVo);

        ApiOrderVo apiOrderVo = apiOrderService.selectOne(orderId);
        if (apiOrderVo != null) {
            apiOrderVo.setCommentStatus(1);
            apiOrderService.updata(apiOrderVo);
        }

        if (labelIds != null) {
            String labelArr[] = labelIds.split(",");
            for (int i = 0; i < labelArr.length; i++) {
                if (!labelArr[i].trim().equals("")) {
                    ApiLabelVo apiLabelVo = apiLabelService.selectOne(Long.valueOf(labelArr[i]));
                    ApiUserLabelVo apiUserLabelVo = new ApiUserLabelVo();
                    apiUserLabelVo.setLabelId(apiLabelVo.getId());
                    apiUserLabelVo.setLabelName(apiLabelVo.getName());
                    apiUserLabelVo.setUserId(userId);
                    apiUserLabelVo.setTvAnchor(userId);

                    apiUserLabelVo.setMouldId(orderId);
                    apiUserLabelVo.setMouldName(mouldName);

                    apiUserLabelService.save(apiUserLabelVo);
                }
            }
        }

        return R.ok(0, "提交成功", "");
    }


}
